package cgl.narada.transport.up2p.messages;

import java.io.IOException;
import java.io.Serializable;

import org.apache.log4j.Logger;

import cgl.narada.util.ObjectMarshaller;

/**
 * 
 * Created on Aug 7, 2006
 * 
 * @author Harshawardhan Gadgil (hgadgil@grids.ucs.indiana.edu)
 */

public class P2PConnectionRequest implements Serializable {

   static Logger log = Logger.getLogger("P2PConnectionRequest");

   private String destinationPeerID;
   private String magic;

   public P2PConnectionRequest(String destId, String magic) {
      destinationPeerID = destId;
      this.magic = magic;
   }

   public final String getDestinationPeerID() {
      return destinationPeerID;
   }

   public final String getMagic() {
      return magic;
   }

   /**
    * Marshalls the object into an array of bytes.
    * 
    * @return an array of bytes representing the marshaller object, null if
    *         error occurs
    */
   public byte[] getBytes() {
      try {
         return ObjectMarshaller.marshall(this);
      } catch (IOException e) {
         log.error("", e);
         return null;
      }
   }

   /**
    * Unmarshalls the bytes as a P2PConnectionRequest object
    * 
    * @param objectBytes
    * @return P2PConnectionRequest if successful, null if error occurs
    */
   public static P2PConnectionRequest createObjectFromBytes(byte[] objectBytes) {
      try {
         return (P2PConnectionRequest) ObjectMarshaller.unmarshall(objectBytes);
      } catch (IOException e) {
         log.error("", e);
      } catch (ClassNotFoundException e) {
         log.error("", e);
      }
      return null;
   }
}
